home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / bother__ / cenvid.zip / CENVIDOS.ZIP / DELOLD.BAT < prev    next >
DOS Batch File  |  1994-10-05  |  5KB  |  172 lines

  1. @echo off
  2. REM *******************************************************
  3. REM *** DelOld.bat - Act like DEL, but delete file that ***
  4. REM *** ver.1        are more than a certain number of  ***
  5. REM ***              days old.                          ***
  6. REM *******************************************************
  7.  
  8. CEnviD %0.bat %1 %2 %3 %4 %5 %6 %7 %8 %9
  9. GOTO CENVI_EXIT
  10.  
  11. Instructions()
  12. {
  13.    puts("\a")
  14.    puts(`DelOld - Delete old file; files more than X days old`)
  15.    puts(``)
  16.    puts(`USAGE: DelOld <FileSpec> <Age> [/P] [/R] [/S] [/H]`)
  17.    puts(``)
  18.    puts(`WHERE: FileSpec - Any file specification, including wildcards`)
  19.    puts(`       Age - Age, in days, of files to delete`)
  20.    puts(`       /P - Prompt before deleting each file`)
  21.    puts(`       /R - Recursive; work in current and subirectories`)
  22.    puts(`       /S - Include files with the SYSTEM attribute`)
  23.    puts(`       /H - Include files with the HIDDEN attribute`)
  24.    puts(``)
  25.    puts(`EXAMPLE: This will delete all *.bak file on C: at least 10 days old`)
  26.    puts(`            DelOld C:\*.bak 10 /R /S /H`)
  27.    puts(``)
  28. }
  29.  
  30. #include <OptParms.lib>
  31.  
  32. Prompt;  // options
  33. AgeInSeconds;
  34. Attrib;
  35. FileSpec;
  36. dta; // disk transfer address
  37.  
  38. main(argc,argv)
  39. {
  40.    // get the command-line options
  41.    Prompt = OptionalParameter(argc,argv,"P");
  42.    Recurse = OptionalParameter(argc,argv,"R");
  43.    System = OptionalParameter(argc,argv,"S");
  44.    Hidden = OptionalParameter(argc,argv,"H");
  45.  
  46.    // get file name and age
  47.    InputFileSpec = argv[1];
  48.    Age = atoi(argv[2]);
  49.    AgeInSeconds = Age * 24 * 60 * 60;
  50.  
  51.    // if parameters weren't valid then print instructions and leave
  52.    if ( argc != 3  ||  Age < 1 ) {
  53.       Instructions();
  54.       return EXIT_FAILURE;
  55.    }
  56.  
  57.    // Build list of matching files
  58.    Attrib = FATTR_RDONLY | FATTR_ARCHIVE;
  59.    if ( System ) Attrib |= FATTR_SYSTEM;
  60.    if ( Hidden ) Attrib |= FATTR_HIDDEN;
  61.  
  62.    // break into directory and file spec
  63.    if ( InitialDir = SplitFileName(InputFileSpec).dir );
  64.    strcpy(FileSpec,InputFileSpec + strlen(InitialDir));
  65.  
  66.    GetDTA();
  67.  
  68.    if ( Recurse )
  69.       DirCount = BuildDirectoryList(DirList,InitialDir);
  70.    // always perform in top directory
  71.    DelOldFilesInDirectory(InitialDir);
  72.    // Perform this operation in every subdirectory of current directory
  73.    if ( Recurse ) {
  74.       for ( i = 0; i < DirCount; i++ ) {
  75.          strcat(DirList[i].name,"\\");
  76.          DelOldFilesInDirectory(DirList[i].name);
  77.       }
  78.    }
  79.  
  80.    return EXIT_SUCCESS;
  81. }
  82.  
  83. BuildDirectoryList(pDirList,pInitialDir)
  84. {
  85.    printf("Building directory list...");
  86.    sprintf(DirSpec,"%s*.*",pInitialDir);
  87.    if ( pDirList = Directory(DirSpec,True,
  88.                             FATTR_RDONLY|FATTR_HIDDEN|FATTR_SYSTEM|FATTR_SUBDIR|FATTR_ARCHIVE,
  89.                             FATTR_SUBDIR) ) {
  90.       DirCount = 1 + GetArraySpan(pDirList);
  91.    } else
  92.       DirCount = 0
  93.    printf(" Done.\n");
  94.    return DirCount;
  95. }
  96.  
  97. DelOldFilesInDirectory(pDir)
  98.    // delete each file matching FileSpec that is at least Age old
  99. {
  100.    FirstFile = True;
  101.    while ( FileName = FindNextFileEntry(FirstFile,pDir,FileTime,FileAttr) ) {
  102.       FirstFile = False;
  103.       if ( AgeInSeconds <= difftime(time(),FileTime) ) {
  104.          DeleteFile(pDir,FileName,FileTime,FileAttr);
  105.       }
  106.    }
  107. }
  108.  
  109. DeleteFile(pDir,pFileName,pFileTime,pAttrib)
  110. {
  111.    strftime(TimeBuf,"%m-%d-%y",localtime(pFileTime));
  112.    // print name of file, date, and age in days
  113.    sprintf(FullFileName,"%s%s",pDir,pFileName);
  114.    printf("%-58s %s Age: %.1f\n",FullFileName,TimeBuf,
  115.           float(difftime(time(),pFileTime)) / (24 * 60 * 60) );
  116.  
  117.    // if rd_only then explain that cannot delete
  118.    if ( FATTR_RDONLY & pAttrib ) {
  119.       printf("\n\a\tCANNOT DELETE READ-ONLY FILE!!!\n");
  120.    } else {
  121.       if ( Prompt ) {
  122.          printf("Delete? (Y/N) ");
  123.          Answer = toupper(getch());
  124.          while ( !strchr("YN",Answer) ) {
  125.             printf("\a");
  126.             Answer = toupper(getch());
  127.          }
  128.          printf("%c\n",Answer);
  129.       }
  130.       if ( !Prompt  ||  Answer == 'Y' )
  131.          remove(FullFileName);
  132.    }
  133. }
  134.  
  135. GetDTA()
  136. {
  137.    reg.ah = 0x2F;
  138.    interrupt(0x21,reg);
  139.    dta = Address(reg.es,reg.bx);
  140. }
  141.  
  142. FindNextFileEntry(pFirstEntry,pDir,pTime,pAttrib)
  143. {
  144.    if ( pFirstEntry ) {
  145.       reg.ah = 0x4E;
  146.       reg.cx = Attrib;
  147.       sprintf(FullFileName,"%s%s",pDir,FileSpec);
  148.       reg.ds = segment(FullFileName);
  149.       reg.dx = offset(FullFileName);
  150.    } else {
  151.       reg.ah = 0x4F;
  152.    }
  153.    if ( !interrupt(0x21,reg) )
  154.       return NULL;
  155.  
  156.    pAttrib = peek(dta+21);
  157.    // determine date in format we recognized
  158.    lDate = peek(dta+24,UWORD16);
  159.    tm.tm_year = 80 + (lDate>>9) & 0x7F;
  160.    tm.tm_mon = ((lDate>>5) & 0xF) - 1;
  161.    tm.tm_mday = lDate & 0x1F;
  162.    lTime = peek(dta+22,UWORD16);
  163.    tm.tm_hour = (lTime>>11) & 1F;
  164.    tm.tm_min = (lTime>>5) & 3F;
  165.    tm.tm_sec = (lTime & 0x1F) * 2;
  166.    pTime = mktime(tm);
  167.    FileNameFound = peek(dta+30,13);
  168.    return FileNameFound;
  169. }
  170.  
  171. :CENVI_EXIT
  172.